/*
 * @Author: Bing
 * @Date: 2024-02-11 21:01:46
 * @LastEditors: Bing
 * @LastEditTime: 2024-02-12 17:51:20
 * @Description: file content
 * @FilePath: \mi365plus\src\services\review.ts
 */
import { http } from '@/utils/http';
import type {
  RequestMenuIdList,
  ResponseReportData,
  ResponseTaskDetailData,
  ResponseWorksData,
  ResponseWorksTableData,
  ResponseWorksTableIdData,
  ResultOutList,
  ReviewRefrigeratorList,
  SearchRefrigerator,
  selectByMenuId,
} from './types/review';
import { stringifyNumbers } from '@/utils/utils';
/**
 * 查询任务审核（即是否有冰箱任务提交）
 * @param workStatus 0全部状态1待提交，2待审核
 * @returns 返回数组 主要是获取id方便接下来查找
 */
export const searchRefrigerator = async (workStatus: number) => {
  return await http<SearchRefrigerator[]>({
    url: '/api/auditWork/auditDetailList',
    method: 'POST',
    data: {
      workStatus: workStatus,
      workTarget: 0,
      pageNumber: 1,
      pageSize: 30,
    },
  });
};
/**
 * 根据id查询任务事项（可以审批）根据operatorDataList下的reportId 进行审批
 * @param workStatus 0全部状态1待提交，2待审核
 * @returns 返回数组ReviewRefrigeratorList[]
 */
export const searchRefrigeratorById = async (workStatus: number) => {
  const list: ReviewRefrigeratorList[] = [];
  const res = await searchRefrigerator(workStatus);
  if (res.code === '0') {
    res.data.forEach(async (e) => {
      const r = await http<ReviewRefrigeratorList>({
        url: '/api/work/exeDetail',
        method: 'POST',
        data: {
          id: BigInt(e.id!).toString(), //使用bigint不然精度丢失
        },
      });
      r.data = {
        ...r.data,
        id: BigInt(r.data.id).toString(),
      };
      r.data.operatorDataList.map((item) => {
        item.reportId = BigInt(item.reportId).toString();
      });
      list.push(r.data);
    });
    return list;
  }
};
/**
 * 一件提交冰箱任务
 * @param reportId 冰箱记录reportId
 * @param auditImgUrl 图片地址，默认空
 * @param auditView 评语，默认空
 * @returns
 */
export const submitRefrigerator = async (
  reportId: string,
  auditImgUrl: string = '',
  auditView: string = '',
) => {
  return await http<string>({
    url: '/api/auditWork/auditSubmit',
    method: 'POST',
    data: {
      auditImgUrl: auditImgUrl,
      auditView: auditView,
      id: reportId,
      workResult: 1,
    },
  });
};

/**
 * 根据菜单id查询任务
 * @param data selectByMenuId 类型
 * @returns ResultOutList[]
 */
export const getSelectByMenuId = async (data: selectByMenuId) => {
  return await http<ResultOutList[]>({
    url: '/api/reportingHandle/handleList',
    method: 'POST',
    data: {
      ...data,
      id: 0,
      isMark: false,
      pageNumber: 1,
      pageSize: 30,
      search: '',
    },
  });
};
/**
 * 根据id审批任务
 * @param data
 * @returns
 */
export const submitMenuIdList = async (
  data: RequestMenuIdList,
  reviewOpinion: string = '',
) => {
  return await http<string>({
    url: '/api/reportingHandle/handleStatus',
    method: 'POST',
    data: {
      id: data.id,
      reviewOpinion: {
        images: [],
        reviewImgId: [],
        reviewOpinion: reviewOpinion,
      },
      updateStatus: 1,
      type: 2,
    },
  });
};
/**
 * 获取图片token
 * @returns img请求token
 */
export const getImgToken = async () => {
  return await http<{ token: string }>({
    url: '/api/qiniu/appCommonToken',
    method: 'POST',
  });
};
/**
 * 查询工作任务清单
 * @returns
 */
export const getWorks = async () => {
  return await http<ResponseWorksData[]>({
    url: '/api/work/exeDetailList',
    method: 'POST',
    data: {
      pageNumber: 1,
      pageSize: 30,
    },
  });
};
/**
 * 根据查询任务清单id获取工作清单表格id
 */
export const getWorksTableId = async (id: string) => {
  return await http<ResponseWorksTableIdData>({
    url: '/api/work/exeDetail',
    method: 'POST',
    data: {
      id: id,
    },
  });
};

/**
 * 根据工作清单表格id创建表格
 */
export const createWorksTable = async (formId: string) => {
  return await http<ResponseWorksTableData>({
    url: '/api/form/getCommonFormInfo',
    method: 'POST',
    data: {
      formId: formId,
    },
  });
};

/**
 *  根据reportID查询冰箱填写记录详情
 * @param reportId
 * @returns
 */
export const getRefrigeratorDetail = async (reportId: string) => {
  return await http<ResponseReportData>({
    url: '/api/form/findCommonReportById',
    method: 'POST',
    data: {
      reportId: reportId,
    },
  });
};

/**
 * 查找审核人
 */
export const getOperatorList = async () => {
  type data = [
    {
      name: string;
      unionCategoryId: string;
      instanceCount: number;
      completeCount: number;
      overtimeCount: number;
    },
  ];
  const res = await http<data>({
    url: '/api/work/exeList',
    method: 'POST',
  });
  res.data.map(async (item) => {
    await http<data>({
      url: '/api/work/exeDetailCategory',
      method: 'POST',
    });
  });
};

/**
 * 任务详情
 * @param id 任务id
 * @returns
 */
export const getTaskDetail = async (id: string) => {
  return await http<ResponseTaskDetailData>({
    url: '/api/work/exeDetail',
    method: 'POST',
    data: {
      id: id,
    },
  });
};
